﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Zombie : MonoBehaviour
{
    private Animator animator;//定义动画播放器
    private Grid currGrid;//网格状态
    private float speed = 6;//僵尸速度
    private bool isattack;
    void Start()
    {
        //随机选择一个行走动画
        animator = GetComponentInChildren<Animator>();
        int rangeWalk = Random.Range(1, 4);
        switch (rangeWalk)
        {
            case 1:
                animator.Play("Zombie");
                break;
            case 2:
                animator.Play("Zombie_Walk2");
                break;
            case 3:
                animator.Play("Zombie_Walk3");
                break;
        }

        GetGridByVerticalNum(0);//初始化纵坐标
    }

    // Update is called once per frame
    void Update()
    {
        Move();
    }

    
    /// <summary>
    /// 随机决定僵尸的纵坐标
    /// </summary>
    /// <param name="verticalNum"></param>
    private void GetGridByVerticalNum(int verticalNum)
    {
        currGrid = GridManager.Instance.GetGridByVerticalNum(verticalNum);
        transform.position = new Vector3(transform.position.x,currGrid.position.y);
    }
    /// <summary>
    /// 移动
    /// </summary>
    private void Move()
    {
        //监测是否存在植物
        if (currGrid == null) return;
        if (currGrid.havePlant
            &&currGrid.CurrPlantBase.transform.position.x<transform.position.x
            &&transform.position.x- currGrid.CurrPlantBase.transform.position.x<0.3f)
        {
            //吃这个植物
            Attack(currGrid.CurrPlantBase);
        }
        transform.Translate((new Vector2(-1.33f, 0) * (Time.deltaTime / 1)) / speed);
    }

    private void Attack(PlantBase plant)
    {
        animator.Play("Zombie_Attack");
    }
}
